<?php
namespace ProjetA\GroupeBundle\Entity;


use Symfony\Component\Validator\ExecutionContext;

use Symfony\Component\Validator\Constraints\Email as Email;
use Symfony\Component\Validator\Constraints\Time as Time;
use Symfony\Component\Validator\Constraints\Url as Url;
use Symfony\Component\Validator\Constraints\MaxLength as MaxLength;
use Symfony\Component\Validator\Constraints\Collection as Collection;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * @author Gaël Francillette francillette.gael@etu.unice.fr
 * ProjetA\GroupeBundle\Entity\Parametre
 * Info: Si l'entité groupe est modifié ( retrait ou ajout d'attribut ) il faudra alors intervenir dans cette classe afin de prendre
 * les nouveaux attributs en compte à partir de la ligne 26 et 49
 * @todo ajouter un élément au tableau 'Text'=>'Titre du champs'
 */

/**
 * ProjetA\GroupeBundle\Entity\Parametre
 *  
 */
class Parametre {
    private $data;
    public function __construct($parameters,$type,$edit=false)
        {
               if ($edit==false) {
                    $this->data[] = array("label"=>"groupe_type","type"=>'hidden',"value"=>$type);
                    $this->data[] = array("label"=>"groupe_nom","type"=>'text',"value"=>"");
                    $this->data[] = array("label"=>"groupe_description","type"=>'textarea',"value"=>"");
                    $publicparam = array(
                    'choices'   => array('0' => 'Privé', '1' => 'Public'),
                    'expanded' => true, 
                    'preferred_choices' => array(1),
                    'label'     => 'Rendre votre groupe Public',
                    'required'  => true);
                    $this->data[] = array("label"=>"groupe_public","type"=>'choice',"value"=>$type,"param"=>$publicparam);
                    foreach ($parameters as $k => $value) {
                        $name = $value->getId();
                        $this->data[] = array("id"=>$name, "label"=>"data_".$value->getLibelle(),"type"=>$value->getType(),"value"=>"");
                        //$this->{$name}= "text";
                        
                    }
                    
                   
                    
                    
                    
                } else {
                    $this->data[] = array("label"=>"groupe_type","type"=>'hidden',"value"=>$edit["groupe_type"]);
                    $this->data[] = array("label"=>"groupe_nom","type"=>'text',"value"=>$edit["groupe_nom"]);
                    $this->data[] = array("label"=>"groupe_description","type"=>'hidden',"value"=>$edit["groupe_description"]);
                    $publicparam = array(
                    'choices'   => array('0' => 'Privé', '1' => 'Public'),
                    'expanded' => true, 
                    'preferred_choices' => array(1),
                    'label'     => 'Rendre votre groupe Public',
                    'required'  => true,
                    'property_path' => false);
                    $this->data[] = array("label"=>"groupe_public","type"=>'choice',"value"=>$edit["groupe_public"],"param"=>$publicparam);
                    
                    foreach ($parameters as $k => $value) {
                        $name = $value->getId();
                        //$pvalue = $value->getValue();
                        $this->data[$name] = array("id"=>$name, "label"=>"data_".$value->getLibelle(),"type"=>$value->getType(),"value"=>$edit["data_".$value->getLibelle()]);
                        //$this->{$name} = $pvalue; 
                    }
                    
                }
        }    
    public function get() { return $this->data; }    
    
    public function getConstraints(){
        $collectionConstraint = new Collection(array());
        $cons = array();
                $cons=array_merge(array('groupe_nom'=> new MaxLength(255)),$cons);
                $cons=array_merge(array('groupe_description'=> new MaxLength(255)),$cons);
        $data = $this->get();      
        foreach ($data as $k => $value){
            switch($value['type']){
                case 'text':
                         $cons=array_merge(array($value['label'] => new MaxLength(255)),$cons);
                break;
            
                case 'email':
                         $cons=array_merge(array($value['label'] => new Email(array('message' => 'adresse email invalide'))),$cons);
                break;
            
                case 'url':
                         $cons=array_merge(array($value['label'] => new Url(array('message' => 'url invalide'))),$cons);
                break;
             
                case 'time':
                         $cons=array_merge(array($value['label'] => new Time(array('message' => 'Heure mal renseignée'))),$cons);
                break;
            
                case 'textarea':
                         $cons=array_merge(array($value['label'] => new MaxLength(255)),$cons);
                break;
            }
                
                
            }
            $collectionConstraint = new Collection($cons);

        return$collectionConstraint;
    }
}

?>
